package org.linlinjava.litemall.admin.job;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.domain.LitemallCoupon;
import org.linlinjava.litemall.db.domain.LitemallCouponUser;
import org.linlinjava.litemall.db.domain.LitemallSpell;
import org.linlinjava.litemall.db.service.LitemallCouponService;
import org.linlinjava.litemall.db.service.LitemallCouponUserService;
import org.linlinjava.litemall.db.service.LitemallSpellService;
import org.linlinjava.litemall.db.util.CouponConstant;
import org.linlinjava.litemall.db.util.CouponUserConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 为防止拼单过期执行出错(todo)
 * 检测拼单过期
 */
@Component
public class SpellJob {
    private final Log logger = LogFactory.getLog(SpellJob.class);

    @Autowired
    private LitemallSpellService litemallSpellService;
    /**
     * 每隔一个小时检查
     * TODO
     * 注意，因为是相隔一个小时检查，因此导致优惠券真正超时时间可能比设定时间延迟1个小时
     */
    @Scheduled(fixedDelay = 60 * 60 * 1000)
    public void checkCouponExpired() {
        logger.info("系统开启任务检查拼单是否已经过期");
        List<LitemallSpell> spells = litemallSpellService.queryExpired();
        System.out.println("错误拼单数"+spells.size());
        for (LitemallSpell spell: spells) {
            litemallSpellService.fail(spell);
        }
        logger.info("系统结束任务检查拼单是否已经过期");
    }

}
